package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.CheckGroupMapper;
import com.itheima.dao.SetmealMapper;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/7-19:37
 * @Version: 1.0.0
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private JedisPool jedisPool;
    @Override
    public void add(Setmeal setmeal) {
        //返回主键id
         setmealMapper.add(setmeal);
        Integer setmealId = setmeal.getId();
//        获取检查组Id集合
        List<Integer> checkgroupIds = setmeal.getCheckgroupIds();
        if(CollectionUtil.isNotEmpty(checkgroupIds)){
//            批量与数据库一次连接
            setmealMapper.setcheckGroupIdAndcheckgroupIdBatch(setmealId,checkgroupIds);
        }
//        成功保存到mysql数据库以后 , 将图片信息保存到redis数据库
        savePicToRedis(setmeal.getImg());
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
//      开始分页查询
        PageHelper.startPage(currentPage,pageSize);
//        这是查询所有的数据
        Page<Setmeal> setmeals = setmealMapper.findPage(queryString);
        return new PageResult(setmeals.getTotal(),setmeals.getResult());

    }

    @Override
    public List<Setmeal> getSetmeal() {

        List<Setmeal> setmealList =  setmealMapper.getSetmeal();
        return setmealList;

    }

    @Override
    public Setmeal findById(String id) {
//        创建对象
        Setmeal setmeal = setmealMapper.findById(id);
//        根据套餐id查询对应的检查组
        List<CheckGroup> checkGroups = setmealMapper.findCheckGroupsBySetmealId(id);
//        遍历检查组 , 根据检查组ID查询所有的检查项
        for (CheckGroup checkGroup : checkGroups) {
//            查询
            List<CheckItem> checkItems = setmealMapper.findCheckItemByCheckGroupId(checkGroup.getId());
//            加进去
            checkGroup.setCheckItems(checkItems);
        }
        setmeal.setCheckGroups(checkGroups);
        return setmeal;

    }

    @Override
    public Map findById4Edit(String  setmealId) {

//        查询数据
        Setmeal setmeal = setmealMapper.findById(setmealId);
//        获取检查组集合
        List<CheckGroup> checkGroups = checkGroupMapper.findAll();
//        查询检查组的ID集合
        List<Integer> checkGroupIds = setmealMapper.findCheckGroupIdsBySetmealId(setmealId);
        Map map = new HashMap<>();
        //        formData: {},//表单数据
//                tableData:[],//添加表单窗口中检查组列表数据
//                checkgroupIds:[],//添加表单窗口中检查组复选框对应id
        map.put("setmeal",setmeal);
        map.put("checkGroups",checkGroups);
        map.put("checkgroupIds",checkGroupIds);
        return map;
    }

    @Override
    public void edit(Setmeal setmeal) {
       setmealMapper.edit(setmeal);
       //获取检测组id集合
       List<Integer> checkgroupIds = setmeal.getCheckgroupIds();
       //更新数据 , 需要将之前勾选的检测项删除, 也就是在中间表中删除关联关系
        setmealMapper.deleteAssociation(setmeal.getId());

       //重新关联
       if(CollectionUtil.isNotEmpty(checkgroupIds)) {
           setmealMapper.setsetmealIdAndcheckGroupIdsBatch(setmeal.getId(), checkgroupIds);
       }
        savePicToRedis(setmeal.getImg());
    }

    @Override
    public void delete(Integer setmealId) {
        //删除应该先删除多表关联的中间表数据
        setmealMapper.deleteAssociation(setmealId);
        setmealMapper.delete(setmealId);
    }

    //    封装方法
    public void savePicToRedis(String picName){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,picName);
        }catch (Exception e){

        }finally {
            jedis.close();
        }


    }
}
